﻿using System;
using System.Collections.Generic;
using NHibernate;
using SimpleScrum.Core.Entities;
using SimpleScrum.Helpers;
using SimpleScrum.Utils;

namespace SimpleScrum.Areas.Scrum.Models
{
    public class BackLogModel
    {
        private ISession _session;

        public IList<BackLog> GetBackLogs(Guid projectId)
        {
            IList<BackLog> backLogs;
            using (_session = NHibernateHelper.GetSession())
            {
                //NHibernate query 
                IQuery query =
                    _session.CreateQuery("from BackLog where ProjectId='" + projectId + "';");
                backLogs = query.List<BackLog>();
            }
            return backLogs;
        }

        public IList<BackLog> GetBackLogs()
        {
            IList<BackLog> backLogs;
            using (_session = NHibernateHelper.GetSession())
            {
                //NHibernate query 
                IQuery query =
                    _session.CreateQuery("from BackLog where ProjectId='" + Client.Instance.CurrentOrganizationId + "';");
                backLogs = query.List<BackLog>();
            }
            return backLogs;
        }

        public BackLog GetBackLogById(Guid id)
        {
            var backLog = new BackLog();
            using (_session = NHibernateHelper.GetSession())
            {
                using (ITransaction tran = _session.BeginTransaction())
                {
                    backLog = _session.Get<BackLog>(id);
                    tran.Commit();
                }
            }
            return backLog;
        }

        public bool NewBackLog(BackLog backLog)
        {
            using (_session = NHibernateHelper.GetSession())
            {
                using (ITransaction tran = _session.BeginTransaction())
                {
                    //backLog.UserStoryId = _currentUser;
                    _session.Save(backLog);
                    tran.Commit();
                }
            }
            return true;
        }

        public void UpdateBackLog(BackLog backLog)
        {
            using (_session = NHibernateHelper.GetSession())
            {
                using (ITransaction tran = _session.BeginTransaction())
                {
                    //backLog.UserStoryId = _currentUser;
                    _session.Update(backLog);
                    tran.Commit();
                }
            }
        }

        public void DeleteBackLog(BackLog backLog)
        {
            using (_session = NHibernateHelper.GetSession())
            {
                using (ITransaction tran = _session.BeginTransaction())
                {
                    try
                    {
                        _session.Delete(backLog);
                        tran.Commit();
                    }
                    catch (Exception ex)
                    {
                        string msg = ex.Message;
                    }
                }
            }
        }
    }
}